package com.scau.yangsuiyu.estore.controller.myComponent;

import com.scau.yangsuiyu.estore.dababases.dao.ItemDao;
import com.scau.yangsuiyu.estore.dababases.dao.OrderDao;
import com.scau.yangsuiyu.estore.dababases.entity.Goods;
import com.scau.yangsuiyu.estore.dababases.entity.Item;
import com.scau.yangsuiyu.estore.dababases.entity.OrderDetail;
import com.scau.yangsuiyu.estore.dababases.entity.Orders;
import com.scau.yangsuiyu.estore.service.AppService;
import com.scau.yangsuiyu.estore.util.ImageViewUtil;
import com.scau.yangsuiyu.estore.util.OrderStatus;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

/**
 * @author suiyu_yang
 * @description 存订单的HBox
 * @date 2019-06-11 21:27
 * @email suiyu_yang@163.com
 */
public class SellerOrderHBox extends HBox implements Initializable {
    private OrderDetail orderDetail;
    private Item item;
    private Orders orders;

    @FXML
    private Label ordersIdLabel;

    @FXML
    private ImageView goodsPhoto;

    @FXML
    private Label goodsNameLabel;

    @FXML
    private Label createTimeLabel;

    @FXML
    private Label ordersStatusLabel;

    @FXML
    private Button deliveryButton;

    @FXML
    private Label promotionalPriceLabel;

    @FXML
    private Label priceLabel;

    public SellerOrderHBox(OrderDetail orderDetail) {
        this.orderDetail = orderDetail;
        this.item = new ItemDao().getItemByItemId(orderDetail.getItemId());
        this.orders = new OrderDao().getOrderByOrderId(orderDetail.getOrderId());
        loadFXML();
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        setOrderDetailToUI();

        if (orders.getStatus().equals(OrderStatus.UN_DELIVERED)) { //未发货
            deliveryButton.setDisable(false);
        } else {
            deliveryButton.setDisable(true);
            deliveryButton.setVisible(false);
        }
        deliveryButton.disableProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                if (newValue) { //最新的button被禁用
                    deliveryButton.setVisible(false);
                } else {
                    deliveryButton.setText("发货");
                }
            }
        });
    }

    private void loadFXML() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/myComponent/SellerOrderHBox.fxml"));
        fxmlLoader.setController(this);
        fxmlLoader.setRoot(this);
        try {
            fxmlLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @FXML
    private void showGoodsDetail() {
        Item item = new ItemDao().getItemByItemId(orderDetail.getItemId());
        Goods goods = item.getGoods();
        new AppService().gotoBuyGoodsPane(null, goods);
    }

    private void setOrderDetailToUI() {
        this.item = new ItemDao().getItemByItemId(orderDetail.getItemId());
        this.orders = new OrderDao().getOrderByOrderId(orderDetail.getOrderId());

        ordersIdLabel.setText(orderDetail.getOrderId() + "");
        ImageViewUtil.loadImage(goodsPhoto, item.getGoods().getGoodsPhotoList().get(0).getPhotoUrl());
        goodsNameLabel.setText(item.getGoods().getGoodsName());
        createTimeLabel.setText(orders.getCreateTime() + "");
        ordersStatusLabel.setText(OrderStatus.statusMap.get(orders.getStatus()));
        promotionalPriceLabel.setText("¥" + item.getPaymentPrice());
        priceLabel.setText("¥" + item.getTotalPrice());
    }

    @FXML
    private void delivery() {
        deliveryButton.setDisable(true);
        orders.setStatus(OrderStatus.UN_RECEIVED);
        new OrderDao().update(orders);
        setOrderDetailToUI();
    }
}
